package com.wgtgt.big_folder2;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.StrUtil;

public class BigFolder {
	private static Map<String, Long> map = new HashMap<>();

	public static void scan(String path) {
		File folder = new File(path);
		scan1(folder);
		Collection<Long> values = map.values();
		Set<Long> hashSet = new HashSet<Long>(values);
		List<Long> list = new ArrayList<Long>(hashSet);
		Collections.sort(list);
		int size = list.size();
		int limit = 10;
		int min = size < limit ? size : limit;
		int elementSize = list.size() - min;
		long limitSize = list.get(elementSize);
		List<Pair<String, Long>> piarList = new ArrayList<>();
		Set<Entry<String, Long>> entrySet = map.entrySet();
		for (Entry<String, Long> entry : entrySet) {
			String key = entry.getKey();
			Long value = entry.getValue();
			if (value >= limitSize) {
				Pair<String, Long> pair = Pair.of(key, value);
				piarList.add(pair);
			}
		}
		piarList.sort(new PairComparator());
		for (int i = 0; i < piarList.size(); i++) {
			Pair<String, Long> pair = piarList.get(i);
			String key = pair.getKey();
			Long value = pair.getValue();
			StringBuilder sb = new StringBuilder();
			sb.append("【大小】");
			sb.append(StrUtil.fillBefore(value + "", ' ', 20));
			sb.append(", ");
			sb.append("【路径】");
			sb.append(key);
			System.out.println(sb.toString());
		}
	}

	private static long scan1(File folder) {
		String key = folder.getAbsolutePath().replace("\\", "/");
		if (folder.isFile()) {
			long length = folder.length();
			map.put(key, length);
			return length;
		} else {
			long total = 0L;
			File[] listFiles = folder.listFiles();
			if (listFiles != null) {
				for (File file : listFiles) {
					long size = 0L;
					size = scan2(file);
					map.put(key, size);
					total += size;
				}
			}
			return total;
		}
	}

	private static long scan2(File folder) {
		String key = folder.getAbsolutePath().replace("\\", "/");
		if (folder.isFile()) {
			long length = folder.length();
			map.put(key, length);
			return length;
		} else {
			File[] listFiles = folder.listFiles();
			long total = 0L;
			if (listFiles != null) {
				for (File file : listFiles) {
					long size = 0L;
					size = scan3(file);
					map.put(key, size);
					total += size;
				}
			}
			return total;
		}
	}

	private static long scan3(File folder) {
		String key = folder.getAbsolutePath().replace("\\", "/");
		if (folder.isFile()) {
			long length = folder.length();
			map.put(key, length);
			return length;
		} else {
			File[] listFiles = folder.listFiles();
			long total = 0L;
			if (listFiles != null) {
				for (File file : listFiles) {
					long size = 0L;
					size = scan(file, size);
					map.put(key, size);
					total += size;
				}
			}
			return total;
		}
	}

	private static long scan(File folder, long size) {
		if (folder.isFile()) {
			long length = folder.length();
			String key = folder.getAbsolutePath().replace("\\", "/");
			map.put(key, length);
			return size + length;
		} else {
			File[] listFiles = folder.listFiles();
			if (listFiles != null) {
				for (File file : listFiles) {
					size = scan(file, size);
				}
			}
		}
		return size;
	}
}
